package com.open.capacity.notice.processor;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.plugin.core.PluginRegistry;
import org.springframework.plugin.core.config.EnablePluginRegistries;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.stereotype.Service;

import com.open.capacity.notice.dmo.SendMessageTask;

import cn.hutool.core.util.StrUtil;

/**
 * 发送去掉+消息类型 选择策略工厂
 *
 */
@Service
@EnablePluginRegistries(CreateTaskProcessor.class)
public class CreateTaskProcessorFactory {

	private static final String ERROR_MSG = "找不到消息code为 %s 类型为%s 的实现类";

	@Resource
	private PluginRegistry<CreateTaskProcessor, SendMessageTask> createTaskProcessorRegistry;

	public CreateTaskProcessor getCreateTaskService(final SendMessageTask sendMessageTask) {
		return Optional.ofNullable(createTaskProcessorRegistry.getPluginFor(sendMessageTask))
				.orElseThrow(() -> new InternalAuthenticationServiceException(
						StrUtil.format(ERROR_MSG, sendMessageTask.getSendType(), sendMessageTask.getMsgType())));

	}

}
